<?php

namespace App\Http\Controllers\Api\Order;
use App\Http\Controllers\ApiController;
use App\Http\Models\User\UserBaseModel;
use App\Models\Balance;
use App\Models\distribution;
use App\Models\Order;
use App\Models\RechargeOrder;
use App\Models\SystemConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NotifyController extends ApiController
{

    public function Notify($order_sn)
    {
        $type = self::getOrderType($order_sn);
        if ($type == 1){

            $flag = $this->NotifyByGoods($order_sn);
        }elseif ($type==2||$type==3){
            $flag = $this->NotifyByRecharge($order_sn);
        }
        return $flag;
    }


    public function NotifyByRecharge($order_sn)
    {
        $order                 = RechargeOrder::where('order_sn',$order_sn)->where('pay_status',0)->first();
        if (empty($order)){
            return false;
        }
        #充值优惠


        #给充值用户加钱
        $flag                   = UserBaseModel::addCoin($order['uid'],$order['total_amount']);

        #成为会员
        #1 会员临界金额
        $user                   = UserBaseModel::getUser($order['uid']);
        $can_vip_amount         = SystemConfig::getConfValByConfKey('recharge_money');
        #充值金额是否满足成为会员条件
        #不是会员 并且 充值金额 满足条件
        if ($user['is_vip'] == 0 && $order['total_amount'] >= $can_vip_amount['conf_val']) {
            UserBaseModel::updateUser($order['uid'],['is_vip'=>1]);
        }

        #给分销用户加钱
        $flag1                  = $this->distributionGiven($order['order_sn']);
        RechargeOrder::where('order_sn',$order_sn)->update(['pay_status'=>1]);
        return $flag&&$flag1;
    }


    public function NotifyByGoods($order_sn)
    {
        $order              = Order::where('order_sn',$order_sn)->where('pay_status',0)->first();
        if (empty($order)){
            return false;
        }
        $order              = Order::getOrderBaseInfoByOrderSn($order_sn);
        if (empty($order)){
            return;
        }
        $flag1              = $this->distributionGiven($order_sn);
        $flag2              = Order::where('order_sn',$order_sn)->update(['pay_status'=>2]);
        Balance::addShoppingRecord($order_sn);
        return $flag1&&$flag2;
    }

    public function testBalance()
    {
        $this->distributionGiven('H904723624993846_V1');
    }


    #分销结算
    public function distributionGiven($order_sn)
    {
        $distribution       = distribution::where('order_sn',$order_sn)->get();
        if (!empty($distribution)){
            $distribution   = $distribution->toArray();
            $flag = 0;
            foreach ($distribution as $k=>$v){
                $data =     [
                    'title'     => '订单'.$order_sn.' 第'.$v['level'].'级'.'收益:'.$v['total_money'].' CNY',
                    'type'      => 2,
                    'origin'    => 2,
                    'uid'       => $v['beneficiary_uid'],
                    'amount'    => $v['total_money'],
                ];
                $flag           += UserBaseModel::addWithdraw($v['beneficiary_uid'],$v['total_money']);
                Balance::addRecord($data);
            }
        }
        #根据订单号判断句 如果是商品订单 不修改状态 收货之后修改  其他 直接修改状态
        if (ApiController::getOrderType($order_sn)!=1){
            distribution::where('order_sn',$order_sn)->update(['status'=>1]);
        }
        return $flag;
    }


}
